#include "CObjectManager.h"
#include "CBase.h"
#include "CHero.h"
#include "CUnit.h"
#include "CStructure.h"
#include "CResource.h"

CObjectManager::CObjectManager()
{
}
CObjectManager::~CObjectManager()
{

}
CObjectManager* CObjectManager::GetInstance()
{
	static CObjectManager instance;
	return &instance;
}
void CObjectManager::Init()
{
}
void CObjectManager::Shutdown()
{
}
void CObjectManager::AddObject(CBase* pObject)
{
	if(pObject == NULL)
		return;

	m_vecStuff.push_back(pObject);
	pObject->AddRef();
}
void CObjectManager::Update(float fDelta)
{
	vector<CBase*>::iterator StuffIter = m_vecStuff.begin();

	while(StuffIter != m_vecStuff.end())
	{
		(*StuffIter)->Update(fDelta);
		++StuffIter;
	}
}
void CObjectManager::CheckCollisons()
{
	vector<CBase*>::iterator Thing = m_vecStuff.begin();

	while(Thing != m_vecStuff.end())
	{
		vector<CBase*>::iterator PotentialHit = m_vecStuff.begin();
		while(PotentialHit != m_vecStuff.end())
		{
			(*Thing)->Collision(*PotentialHit);
			PotentialHit++;
		}
		Thing++;
	}
}
void CObjectManager::CheckDistances(float fDelta)
{
	vector<CBase*>::iterator Me = m_vecStuff.begin();
	while(Me != m_vecStuff.end())
	{
		if((*Me)->GetType() == UNIT)
		{
			vector<CBase*>::iterator MyNeighbor = m_vecStuff.begin();
			while(MyNeighbor != m_vecStuff.end())
			{
				if((*MyNeighbor)->GetType() == UNIT && (*MyNeighbor) != (*Me))
				{
					(*Me)->CheckDistance((*MyNeighbor), fDelta);
					MyNeighbor++;
				}
				else
					MyNeighbor++;
			}
			Me++;
		}
		else
			Me++;
	}
}
void CObjectManager::Render()
{
	vector<CBase*>::iterator StuffIter = m_vecStuff.begin();

	while(StuffIter != m_vecStuff.end())
	{
		(*StuffIter)->Render();
		++StuffIter;
	}
}
void CObjectManager::RemoveObject(CBase *pObject)
{
	if(pObject == NULL)
		return;

	for (vector<CBase*>::iterator Garbage = m_vecStuff.begin(); Garbage != m_vecStuff.end(); Garbage++)
	{
		if ((*Garbage) == pObject)
		{
			(*Garbage)->Release();
			Garbage = m_vecStuff.erase(Garbage);	
			break;
		}
	}
}
void CObjectManager::Clear()
{
	for(unsigned int stuff = 0; stuff < m_vecStuff.size(); ++stuff)
	{
		m_vecStuff[stuff]->Release();
	}
	m_vecStuff.clear();
}
